package TankGame07;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    Vector<Bullet> bulletVector = new Vector<>();
    private boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    Vector<EnemyTank> enemyTanks = new Vector<>();

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public boolean isTouchEnemyTank() {
        switch (this.getDirect()) {
            case 0:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //从集合中取出的坦克对象不是当前对象才做比较
                    if (this != enemyTank) {
                        //考虑敌方坦克方向是上和下与当前坦克做比较
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //该坦克对象的左上角坐标this.getX();this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //该坦克对象的右上角坐标this.getX()+40;this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //考虑敌方坦克方向是左和右与当前坦克做比较
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //该坦克对象的左上角坐标this.getX();this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //该坦克对象的右上角坐标this.getX()+40;this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //从集合中取出的坦克对象不是当前对象才做比较
                    if (this != enemyTank) {
                        //考虑敌方坦克方向是上和下与当前坦克做比较
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //该坦克对象的右上角坐标this.getX()+60;this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX()+60 >= enemyTank.getX()
                                    && this.getX()+60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //该坦克对象的右下角坐标this.getX()+60;this.getY()+40;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX()+60>= enemyTank.getX()
                                    && this.getX()+60 <= enemyTank.getX() + 40
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //考虑敌方坦克方向是左和右与当前坦克做比较
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //该坦克对象的右上角坐标this.getX()+60;this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX()+60 >= enemyTank.getX()
                                    && this.getX()+60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //该坦克对象的右下角坐标this.getX()+60;this.getY()+40;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX()+60 >= enemyTank.getX()
                                    && this.getX()+60 <= enemyTank.getX() + 60
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //从集合中取出的坦克对象不是当前对象才做比较
                    if (this != enemyTank) {
                        //考虑敌方坦克方向是上和下与当前坦克做比较
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //该坦克对象的左下角坐标this.getX();this.getY()+60;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY()+60 >= enemyTank.getY()
                                    && this.getY()+60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //该坦克对象的右下角坐标this.getX()+40;this.getY()+60;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 40
                                    && this.getY()+60 >= enemyTank.getY()
                                    && this.getY()+60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //考虑敌方坦克方向是左和右与当前坦克做比较
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //该坦克对象的左下角坐标this.getX();this.getY()+60;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY()+60 >= enemyTank.getY()
                                    && this.getY()+60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //该坦克对象的右下角坐标this.getX()+60;this.getY()+40;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX()+40 >= enemyTank.getX()
                                    && this.getX()+40 <= enemyTank.getX() + 60
                                    && this.getY()+60 >= enemyTank.getY()
                                    && this.getY()+60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //从集合中取出的坦克对象不是当前对象才做比较
                    if (this != enemyTank) {
                        //考虑敌方坦克方向是上和下与当前坦克做比较
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            //该坦克对象的左上角坐标this.getX();this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            //该坦克对象的左下角坐标this.getX();this.getY()+40;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+40
                            //            enemyTank.getY() enemyTank.getY()+60
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        //考虑敌方坦克方向是左和右与当前坦克做比较
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            //该坦克对象的左上角坐标this.getX();this.getY();
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            //该坦克对象的左下角坐标this.getX();this.getY()+40;
                            //敌人坦克范围是enemyTank.getX() enemyTank.getX()+60
                            //            enemyTank.getY() enemyTank.getY()+40
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY()+40 >= enemyTank.getY()
                                    && this.getY()+40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        while (true) {
            //发射多颗子弹
            if (isLive && bulletVector.size() <= 5) {
                //限制子弹只能发射五颗
                Bullet bullet = null;
                //敌方坦克存活,并且子弹集合里面已经没有子弹
                //就重新创建子弹线程，然后启动线程
                switch (getDirect()) {
                    case 0:
                        bullet = new Bullet(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        bullet = new Bullet(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        bullet = new Bullet(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        bullet = new Bullet(getX(), getY() + 20, 3);
                        break;
                }
                bulletVector.add(bullet);//添加进集合里
                bullet.start();//启动子弹线程
            }

            //坦克根据方向来继续移动
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 5; i++) {
                        if (getY() > 0 && !isTouchEnemyTank())
                            moveUp();
                        if (getY() == 0) {
                            break;
                        }
//                        else if (getY() == 0) {
//                            moveDown();
//                            setDirect(2);
//                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 5; i++) {
                        if (getX() + 60 < 1000 && !isTouchEnemyTank())
                            moveRight();
                        if (getX() + 60 == 500) {
                            break;
                        }
//                        else if (getX() == 940) {
//                            moveLeft();
//                            setDirect(3);
//                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 5; i++) {
                        if (getY() + 60 < 750 && !isTouchEnemyTank())
                            moveDown();
                        if (getY() + 60 == 750) {
                            break;
                        }
//                        else if (getY() == 690) {
//                            moveUp();
//                            setDirect(0);
//                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++ ) {
                        if (getX() > 0 && !isTouchEnemyTank())
                            moveLeft();
                        if (getX() == 0) {
                            break;
                        }
//                        else if (getX() == 0) {
//                            moveRight();
//                            setDirect(1);
//                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            //让坦克随机移动
            //生成0-3的一个随机数
            int random = (int) (Math.random() * 4);
            //让每个坦克对象随机改变一个方向，下次进入循环的时候
            setDirect(random);
            if (isLive == false) {
                break;
            }
        }
    }

    public Vector<Bullet> getBulletVector() {
        return bulletVector;
    }

    public void setBulletVector(Vector<Bullet> bulletVector) {
        this.bulletVector = bulletVector;
    }

    public boolean getIsLive() {
        return isLive;
    }

    public void setIsLive(boolean live) {
        isLive = live;
    }
}
